<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>门店管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 面包屑下面的文字 -->
    <el-row style="margin-top: 10px">
      <el-button type="text" @click="storeList" style="font-size: 18px"
        >全部门店</el-button
      >
      <el-button type="text" @click="storeList" style="font-size: 18px"
        >上架中</el-button
      >
      <el-button type="text" @click="storeList" style="font-size: 18px"
        >已下架</el-button
      >
    </el-row>

    <!-- 搜索行 -->
    <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
      <el-form-item label="门店名称:">
        <el-input
          v-model="queryInfo.keyword"
          placeholder="门店名称"
          @clear="clearInput"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="门店手机号:">
        <el-input
          clearable
          @clear="clearInput"
          v-model="queryInfo.phone"
          placeholder="门店手机号"
        ></el-input>
      </el-form-item>

      <el-form-item label="提交日期:">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="datevalue"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="storeList">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-button type="primary" @click="addMendian">新增门店</el-button>
    </el-row>

    <el-table :data="storelist" style="width: 100%">
      <el-table-column prop="shopName" label="门店" width="120">
      </el-table-column>
      <el-table-column prop="shopId" label="ID" width="120"> </el-table-column>
      <el-table-column prop="shopPhone" label="手机号" width="120">
      </el-table-column>
      <el-table-column prop="shopAddress" label="门店地址" width="120">
      </el-table-column>
      <el-table-column prop="shopDescribe" label="营业时间" width="120">
      </el-table-column>
      <el-table-column prop="shopAccount" label="关联小区" width="120">
      </el-table-column>
      <el-table-column prop="hopOrderCount" label="订单总数" width="120">
      </el-table-column>
      <el-table-column prop="distance" label="提交方式" width="120">
      </el-table-column>
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.shopStatus == 1"
            >上架中</el-button
          >
          <el-button type="text" v-if="scope.row.shopStatus == 2"
            >下架中</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text">门店管理</el-button><br />
          <el-button type="text" @click="updateInfo(scope.row)">修改门店资料</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :total="newtotal"
    >
    </el-pagination>

    <!-- 新增门店对话框 -->

    <el-dialog title="新增门店" :visible.sync="dialogFormVisible" @close="quxiao">
      <el-form :model="mendian">
        <el-form-item label="门店名称" label-width="100px">
          <el-input
            v-model="mendian.shopName"
            placeholder="门店名称"
            style="width: 220px"
          ></el-input>
        </el-form-item>

        <el-form-item label="门店手机号" label-width="100px">
          <el-input
            v-model="mendian.shopPhone"
            placeholder="门店手机号"
            style="width: 220px"
          >
          </el-input>
        </el-form-item>
        <!-- 级联表 -->
        <el-form-item label="所属地区" label-width="100px">
          <el-cascader v-model="address" :options="citys"></el-cascader>
        </el-form-item>

        <el-form-item label-width="100px">
          <el-input
            v-model="mendian.shopAddress"
            placeholder="请输入详细地址"
          ></el-input>
        </el-form-item>

        <el-form-item label="营业时间" label-width="100px">
          <el-row>
            <el-button type="success" plain @click="getLocation">SOS快获取位置</el-button>
            <span >{{ mendian.shopLatitude }},{{ mendian.shopLongitude }}</span>
          </el-row>
        </el-form-item>

        <el-form-item label="营业时间" label-width="100px">
          <el-input
            v-model="mendian.shopDescribe"
            placeholder="营业时间"
            style="width: 220px"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取消</el-button>
        <el-button type="primary" @click="baocun">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script scoped>
import citys from "../assets/citydata.js";
export default {
  data() {
    return {
      mendian: {
        shopName: "",
        shopPhone: "",
        shopImage: "",
        shopDescribe: "",
        shopAddress: "",
        shopProvince: "",
        shopCity: "",
        shopDistrict: "",
        shopLatitude: "",
        shopLongitude: "",
      },
      biaotou: "",
      citys: citys,
      address:[],
      dialogFormVisible: false,
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        phone: "",
        keyword: "",
        startTime: "",
        endTime: "",
        keyword: "",
      },
      newtotal: 1,
      datevalue: "",
      storelist: [],
    };
  },
  methods: {
    //门店修改信息
    updateInfo(info){
      this.dialogFormVisible= true;
      this.biaotou='编辑门店';
      this.mendian=info;
      this.address=[this.mendian.shopProvince,this.mendian.shopCity,this.mendian.shopDistrict]
    },
    //获取位置
    getLocation(){
      this.$http.get('https://restapi.amap.com/v3/geocode/geo',
        {
          params:{
            key: '457886b5972a97e93fb9cdccebfd450f',
            address:this.mendian.shopProvince+this.mendian.shopCity+this.mendian.shopDistrict+this.mendian.shopAddress
          }
        }
      )
      .then(res=>{
        let location=res.data.geocodes[0].location;
        this.mendian.shopLatitude=location.split(',')[0];
        this.mendian.shopLongitude=location.split(',')[1]; 
      })
      .catch(error=>{
        this.$message.error('GET叽咯');
      })
    },
    
    // 保存按钮
    baocun(){
      if(this.biaotou==='门店新增'){
        this.$http.post('/shop/addShop',this.mendian)
        .then(res=>{
        if(res.data.status==200){
          this.$message.success(res.data.message);
          this.storeList();
          this.dialogFormVisible=false;
          this.quxiao();
        }else{
          this.$message.error(res.data.message)
        }
      })
      .catch(error=>{
        this.$message.error('POST叽叽')
      })
      }else{
        this.$http.post('/shop/modifyShop',this.mendian)
        .then(res=>{
        if(res.data.status==200){
          this.$message.success(res.data.message);
          this.storeList();
          this.dialogFormVisible=false;
          this.quxiao();
        }else{
          this.$message.error(res.data.message)
        }
      })
      .catch(error=>{
        this.$message.error('POST叽叽')
      })
      }
      

    },
    // 取消按钮
    quxiao(){
      this.dialogFormVisible= false;
      this.address=[];
      this.mendian= {
        shopName: "",
        shopPhone: "",
        shopImage: "",
        shopDescribe: "",
        shopAddress: "",
        shopProvince: "",
        shopCity: "",
        shopDistrict: "",
        shopLatitude: "",
        shopLongitude: "",
      };
    },
    // 添加按钮
    addMendian() {
      this.dialogFormVisible = true;
      this.biaotou = "门店新增";
    },
    //增加门店对话框的函数上面
    clearInput() {
      this.storeList();
    },

    storeList() {
      this.$http
        .get("shop/querySearchShopList", {
          params: this.queryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.storelist = res.data.data.list;
            this.newtotal = res.data.data.total;
          } else {
            this.$message.error("请求错误");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.queryInfo.pageNum = 1;
      this.storeList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;
      this.storeList();
    },
  },

  mounted() {
    this.storeList();
  },
  watch: {
    datevalue() {
      this.queryInfo.startTime = this.datevalue[0];
      this.queryInfo.endTime = this.datevalue[1];
    },
    address(){
      this.mendian.shopProvince=this.address[0];
      this.mendian.shopCity=this.address[1];
      this.mendian.shopDistrict=this.address[2];
    }
  },
};
</script>

<style>
</style>